package com.sino.config.server.service.project;

import java.util.List;

import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.plugin.spring.boot.service.BaseService;
import org.nutz.plugin.spring.boot.service.entity.PageredData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sino.config.server.bean.project.Application;
import com.sino.config.server.service.acl.UserService;

/**
 * @author kerbores
 *
 */
@Service
public class ApplicationService extends BaseService<Application> {

	@Autowired
	UserService userService;

	public PageredData<Application> listByUser(int page) {
		List<Application> applications = searchObj(create("list.application.by.user")
				.setParam("userId", userService.curr() == null ? 0 : userService.curr().getId())
				.setParam("pageStart", (page - 1) * PAGESIZE)
				.setParam("pageSize", PAGESIZE));
		Sql sql = create("count.application.by.user")
				.setParam("userId", userService.curr() == null ? 0 : userService.curr().getId())
				.setCallback(Sqls.callback.integer());
		dao().execute(sql);
		PageredData<Application> target = new PageredData<>();
		target.setDataList(applications);
		Pager pager = new Pager(page, PAGESIZE);
		pager.setRecordCount(sql.getInt());
		target.setPager(pager);
		return target;
	}

}
